local net_ushortint = GLOBAL.net_ushortint
local net_bool = GLOBAL.net_bool
local net_string = GLOBAL.net_string

local function SetDirty(netvar, val)
	netvar:set_local(val)
	netvar:set(val)
end

local function OnMusha_SpellpowerDelta(parent, data)
	if data.overtime then
	elseif data.newpercent > data.oldpercent then
		SetDirty(parent.player_classified.ismusha_spellpowerpulseup, true)
	elseif data.newpercent < data.oldpercent then
		SetDirty(parent.player_classified.ismusha_spellpowerpulsedown, true)
	end
end

local function OnMusha_SpellpowerDirty(inst)
	if inst._parent ~= nil then
		local oldpercent = inst._oldmusha_spellpowerpercent
		local percent = inst.currentmusha_spellpower:value() / inst.maxmusha_spellpower:value()
		local data = {
			oldpercent = oldpercent,
			newpercent = percent,
			overtime = not (inst.ismusha_spellpowerpulseup:value() and percent > oldpercent) and
				not (inst.ismusha_spellpowerpulsedown:value() and percent < oldpercent)
		}
		inst._oldmusha_spellpowerpercent = percent
		inst.ismusha_spellpowerpulseup:set_local(false)
		inst.ismusha_spellpowerpulsedown:set_local(false)
		inst._parent:PushEvent("musha_spellpowerdelta", data)
	else
		inst._oldmusha_spellpowerpercent = .75
		inst.ismusha_spellpowerpulseup:set_local(false)
		inst.ismusha_spellpowerpulsedown:set_local(false)
	end
end

local function OnMusha_FatiguesleepDelta(parent, data)
	if data.overtime then
	elseif data.newpercent > data.oldpercent then
		SetDirty(parent.player_classified.ismusha_fatiguesleeppulseup, true)
	elseif data.newpercent < data.oldpercent then
		SetDirty(parent.player_classified.ismusha_fatiguesleeppulsedown, true)
	end
end

local function OnMusha_FatiguesleepDirty(inst)
	if inst._parent ~= nil then
		local oldpercent = inst._oldmusha_fatiguesleeppercent
		local percent = inst.currentmusha_fatiguesleep:value() / inst.maxmusha_fatiguesleep:value()
		local data = {
			oldpercent = oldpercent,
			newpercent = percent,
			overtime = not (inst.ismusha_fatiguesleeppulseup:value() and percent > oldpercent) and
				not (inst.ismusha_fatiguesleeppulsedown:value() and percent < oldpercent)
		}
		inst._oldmusha_fatiguesleeppercent = percent
		inst.ismusha_fatiguesleeppulseup:set_local(false)
		inst.ismusha_fatiguesleeppulsedown:set_local(false)
		inst._parent:PushEvent("musha_fatiguesleepdelta", data)
	else
		inst._oldmusha_fatiguesleeppercent = 1
		inst.ismusha_fatiguesleeppulseup:set_local(false)
		inst.ismusha_fatiguesleeppulsedown:set_local(false)
	end
end

local function OnMusha_StaminasleepDelta(parent, data)
	if data.overtime then
	elseif data.newpercent > data.oldpercent then
		SetDirty(parent.player_classified.ismusha_staminasleeppulseup, true)
	elseif data.newpercent < data.oldpercent then
		SetDirty(parent.player_classified.ismusha_staminasleeppulsedown, true)
	end
end

local function OnMusha_StaminasleepDirty(inst)
	if inst._parent ~= nil then
		local oldpercent = inst._oldmusha_staminasleeppercent
		local percent = inst.currentmusha_staminasleep:value() / inst.maxmusha_staminasleep:value()
		local data = {
			oldpercent = oldpercent,
			newpercent = percent,
			overtime = not (inst.ismusha_staminasleeppulseup:value() and percent > oldpercent) and
				not (inst.ismusha_staminasleeppulsedown:value() and percent < oldpercent)
		}
		inst._oldmusha_staminasleeppercent = percent
		inst.ismusha_staminasleeppulseup:set_local(false)
		inst.ismusha_staminasleeppulsedown:set_local(false)
		inst._parent:PushEvent("musha_staminasleepdelta", data)
	else
		inst._oldmusha_staminasleeppercent = .75
		inst.ismusha_staminasleeppulseup:set_local(false)
		inst.ismusha_staminasleeppulsedown:set_local(false)
	end
end

local function musha_skillmenuSwitch(inst, data)
	inst.skill_menu_shown = not inst.skill_menu_shown
	inst.musha_skills_menu:set((inst.skill_menu_shown and 1 or 0) .. data)
end

local function Musha_SkillmenuDirty(inst)
	local data = string.sub(inst.musha_skills_menu:value(), 2)
	local split_index = string.find(data, " ")
	local data = {
		found_treasure_count = string.sub(data, 1, split_index),
		all_treasure_count = string.sub(data, split_index)
	}
	Skill_UI(inst.entity:GetParent(), data)
end

AddPrefabPostInit(
	"player_classified",
	function(inst)
		inst._oldmusha_spellpowerpercent = 1
		inst.currentmusha_spellpower = net_ushortint(inst.GUID, "musha_spellpower.current", "musha_spellpowerdirty")
		inst.maxmusha_spellpower = net_ushortint(inst.GUID, "musha_spellpower.max", "musha_spellpowerdirty")
		inst.ismusha_spellpowerpulseup = net_bool(inst.GUID, "musha_spellpower.dodeltaovertime(up)", "musha_spellpowerdirty")
		inst.ismusha_spellpowerpulsedown =
			net_bool(inst.GUID, "musha_spellpower.dodeltaovertime(down)", "musha_spellpowerdirty")
		inst.currentmusha_spellpower:set(75)
		inst.maxmusha_spellpower:set(100)

		inst._oldmusha_fatiguesleeppercent = 1
		inst.currentmusha_fatiguesleep = net_ushortint(inst.GUID, "musha_fatiguesleep.current", "musha_fatiguesleepdirty")
		inst.maxmusha_fatiguesleep = net_ushortint(inst.GUID, "musha_fatiguesleep.max", "musha_fatiguesleepdirty")
		inst.ismusha_fatiguesleeppulseup =
			net_bool(inst.GUID, "musha_fatiguesleep.dodeltaovertime(up)", "musha_fatiguesleepdirty")
		inst.ismusha_fatiguesleeppulsedown =
			net_bool(inst.GUID, "musha_fatiguesleep.dodeltaovertime(down)", "musha_fatiguesleepdirty")
		inst.currentmusha_fatiguesleep:set(0)
		inst.maxmusha_fatiguesleep:set(100)

		inst._oldmusha_staminasleeppercent = 1
		inst.currentmusha_staminasleep = net_ushortint(inst.GUID, "musha_staminasleep.current", "musha_staminasleepdirty")
		inst.maxmusha_staminasleep = net_ushortint(inst.GUID, "musha_staminasleep.max", "musha_staminasleepdirty")
		inst.ismusha_staminasleeppulseup =
			net_bool(inst.GUID, "musha_staminasleep.dodeltaovertime(up)", "musha_staminasleepdirty")
		inst.ismusha_staminasleeppulsedown =
			net_bool(inst.GUID, "musha_staminasleep.dodeltaovertime(down)", "musha_staminasleepdirty")
		--这里太玄学了，在 musha_statusdisplays.lua 中绕过解决
		inst.currentmusha_staminasleep:set(100)
		inst.maxmusha_staminasleep:set(100)

		inst.musha_skills_menu = net_string(inst.GUID, "musha_skills.skillmenu", "musha_skillmenudirty")

		inst:DoTaskInTime(
			0,
			function(inst)
				if GLOBAL.TheNet:GetIsServer() then
					inst._parent = inst.entity:GetParent()
					inst:ListenForEvent("musha_spellpowerdelta", OnMusha_SpellpowerDelta, inst._parent)
					inst:ListenForEvent("musha_fatiguesleepdelta", OnMusha_FatiguesleepDelta, inst._parent)
					inst:ListenForEvent("musha_staminasleepdelta", OnMusha_StaminasleepDelta, inst._parent)
					inst:ListenForEvent("musha_skillmenuswitch", musha_skillmenuSwitch)
				else
					inst.ismusha_spellpowerpulseup:set_local(false)
					inst.ismusha_fatiguesleeppulseup:set_local(false)
					inst.ismusha_staminasleeppulseup:set_local(false)
					inst.ismusha_spellpowerpulsedown:set_local(false)
					inst.ismusha_fatiguesleeppulsedown:set_local(false)
					inst.ismusha_staminasleeppulsedown:set_local(false)
					inst:ListenForEvent("musha_spellpowerdirty", OnMusha_SpellpowerDirty)
					inst:ListenForEvent("musha_fatiguesleepdirty", OnMusha_FatiguesleepDirty)
					inst:ListenForEvent("musha_staminasleepdirty", OnMusha_StaminasleepDirty)
					inst:ListenForEvent("musha_skillmenudirty", Musha_SkillmenuDirty)
					if inst._parent ~= nil then
						inst._oldmusha_spellpowerpercent =
							inst.maxmusha_spellpower:value() > 0 and inst.currentmusha_spellpower:value() / inst.maxmusha_spellpower:value() or
							0
						inst._oldmusha_fatiguesleeppercent =
							inst.maxmusha_fatiguesleep:value() > 0 and
							inst.currentmusha_fatiguesleep:value() / inst.maxmusha_fatiguesleep:value() or
							0
						inst._oldmusha_staminasleeppercent =
							inst.maxmusha_staminasleep:value() > 0 and
							inst.currentmusha_staminasleep:value() / inst.maxmusha_staminasleep:value() or
							1
					end
				end
			end
		)
	end
)

AddReplicableComponent("musha_spellpower")
AddReplicableComponent("musha_fatiguesleep")
AddReplicableComponent("musha_staminasleep")
AddReplicableComponent("musha_skills")
